// ==UserScript==
// @name        Steam Market Quick Buy Button
// @namespace   steam
// @include     http://steamcommunity.com/market/listings/*
// @match       http://steamcommunity.com/market/listings/*
// @version     1.0.0
// @grant       none
// ==/UserScript==

(function (){

try
{
  // Activate the clicks 
  function triggerMouseEvent(node, eventType) {
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent(eventType, true, true);
    node.dispatchEvent(clickEvent);
  }

  // Called when the Quick Buy Button is clicked
  function quick_buy_click() {
    triggerMouseEvent($J('.market_commodity_buy_button') [0], 'click');
    document.getElementById('market_buy_commodity_input_price').value = "0.04" ;
    document.getElementById('market_buy_commodity_input_quantity').value = "5" ;
    document.getElementById('market_buynow_dialog_accept_ssa').checked = true ;
    triggerMouseEvent($J('#market_buynow_dialog_purchase') [0], 'click');
  }

  // Called when the Quick Sell Button is clicked
  function quick_sell_click() {
    alert("This Has Not Yet Been Implemented");
  }


  // Add a Quick Buy Button.
  // If we add to '.market_commodity_orders_header', it adds the button to both buy & sell 
  // So, we add the button to the neighbour
  var btnBuy = $J('<a href="#" class="btn_green_white_innerfade btn_medium market_commodity_buy_button"> <span>Quick Buy</span> </a>') ;
  $J(btnBuy) .click(quick_buy_click);
  $J('#market_commodity_forsale_table').siblings('.market_commodity_orders_header').append(btnBuy) ;

  // Add a Quick Sell Button.
  // If we add to '.market_commodity_orders_header', it adds the button to both buy & sell 
  // So, we add the button to the neighbour
  var btnSell = $J('<a href="#" class="btn_green_white_innerfade btn_medium market_commodity_buy_button"> <span>Quick Sell</span> </a>') ;
  $J(btnSell) .click(quick_sell_click);
  $J('#market_commodity_buyreqeusts_table').siblings('.market_commodity_orders_header').append(btnSell) ;

     // $J('.market_commodity_orders_header').append(btn);
  // $J('.market_commodity_orders_header')[1].append(btn);
  // $J('.market_commodity_orders_header')[1].appendChild(btn);
}
catch (err)
{
    window.alert(err);
}

})();
